#include "MCEllipse2D.h"
#include <MCVector2.h>
#include <math.h>

MCEllipse2D::MCEllipse2D() :
    _center(MCPoint2D()),
    _rx(0),
    _ry(0)
{
}

MCEllipse2D::MCEllipse2D(MCPoint2D center, double rx, double ry) :
    _center(center),
    _rx(std::abs(rx)),
    _ry(std::abs(ry))
{
}

MCEllipse2D::MCEllipse2D(const MCEllipse2D &ellipse) :
    _center(ellipse._center),
    _rx(std::abs(ellipse._rx)),
    _ry(std::abs(ellipse._ry))
{
}

MCPlaneItemType MCEllipse2D::type()
{
    return MCPlaneItemType::ELLIPSE;
}

void MCEllipse2D::setPositionAt(const MCPoint2D &at)
{
    _center.setPositionAt(at);
}

void MCEllipse2D::move(const MCVector2 &to)
{
    _center.move(to);
}

void MCEllipse2D::scale(double scale)
{
    _center.scale(scale);
    _rx *= scale;
    _ry *= scale;
}

void MCEllipse2D::transform(const MCMatrix3 &transform)
{
    _center.transform(transform);

    /// ??? need tests
    MCPoint2D tempx = _center + MCPoint2D(_rx, 0);
    _rx = MCVector2((tempx * transform - _center)).magnitude();

    MCPoint2D tempy = _center + MCPoint2D(_ry, 0);
    _rx = MCVector2((tempy * transform - _center)).magnitude();
}

void MCEllipse2D::update(const MCPoint2D &center, double rx, double ry)
{
    _center = center;
    _rx = rx;
    _ry = ry;
}

MCPoint2D MCEllipse2D::center() const
{
    return _center;
}

double MCEllipse2D::rx() const
{
    return _rx;
}

double MCEllipse2D::ry() const
{
    return _ry;
}
